package com.tdkj.action.backstage.information;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;

import com.tdkj.action.BaseAction;
import com.tdkj.model.information.BeanBaseInfo;
import com.tdkj.model.information.BeanExpert;
import com.tdkj.model.user.BeanUser;
import com.tdkj.service.information.IBeanExpertService;
public class ExpertAction  extends BaseAction{
	private Long id;
	private String status;
	private BeanExpert expert=new BeanExpert();
	private List<BeanExpert> experts=new ArrayList<BeanExpert>();
	private IBeanExpertService expertService;
	private String[] ids;
	public String deleteAll() {
		if (null != ids) {
			for (String idtemp : ids) {
				this.getBaseService().delete(Long.parseLong(idtemp), BeanExpert.class);
			}
		}
		return findAll();
	}
	public String[] getIds() {
		return ids;
	}

	public void setIds(String[] ids) {
		this.ids = ids;
	}
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}
	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public BeanExpert getExpert() {
		return expert;
	}

	public void setExpert(BeanExpert expert) {
		this.expert = expert;
	}

	public List<BeanExpert> getExperts() {
		return experts;
	}

	public void setExperts(List<BeanExpert> experts) {
		this.experts = experts;
	}

	public IBeanExpertService getExpertService() {
		return expertService;
	}
	@Resource
	public void setExpertService(IBeanExpertService expertService) {
		this.expertService = expertService;
	}

	//获取专家列表
	public String findAll(){
		Criteria criteria = this.createCriteria(BeanExpert.class);
		criteria.addOrder(Order.desc("experttype"));// 对状态进行排序
		this.queryByPage(criteria, "expertAction!findAll.action", 10);
		return "findAll";
	}

	//添加专家
	public String findAddPage(){
		return "addPage";
	}

	//添加专家
	public String findEditPage(){
		return "editPage";
	}
	
	//添加专家
	public String addExpert(){
		expertService.save(this.getExpert());
		return findAll();
	}
	
	//编辑专家
	public String editExpert(){
		BeanExpert bean=expertService.findById(this.getId(), BeanExpert.class);
		bean=this.getExpert();
		expertService.merge(bean);
		return findAll();
	}
	
	public String deleteExpert(){
		expert.setId(this.getId());
		expertService.delete(expert);
		return findAll();
	}
	
	//处理状态
	public String updateStatus(){
		expertService.updateStatus(this.getId(),this.getStatus());
		return findAll();
	}
	private  void appendObj(int num) {
		// TODO Auto-generated method stub
		BeanExpert bean=null;
		List<BeanUser> u=expertService.findAll(BeanUser.class);
		for(int i=0;i<u.size();i++){
			BeanUser ub=u.get(i);
			bean=new BeanExpert();
			bean.setExpertname("ename"+i);
			bean.setExperttype("t"+i);
			bean.setStatus("专家");
			bean.setUser(ub);
			expertService.save(bean);
		}
		List<BeanExpert> l=expertService.findAll(BeanExpert.class);
		for(int j=0;j<l.size();j++){
			BeanExpert e=l.get(j);
			BeanBaseInfo article=new BeanBaseInfo();
			article.setTitle("good!article");
			article.setUser(e.getUser());
			article.setExpert(e);
			article.setColumntype("2");
			article.setModuletype("11");
			article.setCreatetime(new Timestamp(System.currentTimeMillis()));
			expertService.save(article);
		}
			
	}
	
}
